export default {
	//图片预览
	previewImg(current, imgs) {
		uni.previewImage({
			current: current,
			urls: imgs,
		});
	},

	//退出
	outFun() {
		uni.clearStorageSync()
		uni.switchTab({
			url: '/pages/home/index'
		})
	},

	//没有登录或者登录失效 清除缓存 跳转到首页
	noTokenGo() {
		this.removeStorageSyncVal('token');
		this.removeStorageSyncVal('userInfo');
		uni.removeTabBarBadge({
			index: 2
		})
		uni.hideLoading()
		const platform = uni.getSystemInfoSync().platform
		console.log(platform)

		uni.navigateTo({
			url: '/pages/login/login'
		});
		return;
	},

	//移除指定缓存
	removeStorageSyncVal: function (key) {
		try {
			uni.removeStorageSync(key);
		} catch (e) {
			// error
		}
	},

	//返回
	appBack: function (msg, delta = 1, time = 1300) {
		if (msg) {
			this.msg(msg);
		}
		setTimeout(function () {
			uni.navigateBack({
				delta: delta
			});
		}, time);
	},

	//跳转页面
	href: function (url, options = {}) {
		const {
			type = 1,//跳转方式
			//1：navigateTo 保留当前页面，跳转到应用内的某个页面。(不可跳转到tabBar页面)
			//2：redirectTo 关闭当前页面，跳转到应用内的某个页面。(不可跳转到tabBar页面)
			//3：reLaunch   关闭所有页面，打开到应用内的某个页面。(可跳转到tabBar页面)
			//4：switchTab  跳转到 tabBar 页面，并关闭其他所有非 tabBar 页面。
			ver = false,//验证是否有该路由地址
			params = false,//跳转携带参数
			time = 0,//延迟跳转
			msg = '',//提示文字
			login = ''//验证登录
		} = options;

		const token = uni.getStorageSync('token');

		if (!token && login) {
			this.goLogin();
			return false;
		}

		if (ver && !url) {
			this.msg('Empty URL');
			return false;
		}

		if (params) {
			url = url + '?' + Object.entries(params).map(([key, value]) => `${key}=${value}`).join('&');
		}

		if (msg) {
			uni.showToast({
				title: msg,
				icon: 'none',
				mask: true,
				duration: time || 3000
			});
		}

		setTimeout(() => {
			switch (type) {
				case 1:
					uni.navigateTo({ url });
					break;
				case 2:
					uni.redirectTo({ url });
					break;
				case 3:
					uni.reLaunch({ url });
					break;
				case 4:
					uni.switchTab({ url });
					break;
			}
		}, time);
	},

	//分页状态
	pageState: function (list, data, limit = 10) {
		//list：列表所有数据条数
		//limit：每页多少条
		//data：本次请求返回多少条数据
		let status;
		if (list == 0) {
			status = 'noData';
		} else if (list % limit > 0 || (list % limit == 0 && data == 0)) {
			status = 'noMore';
		} else {
			status = 'more';
		}
		return status;
	},

	//msg提示
	msg: function (title, duration = 1300, mask = false, icon = 'none') {
		//统一提示方便全局修改
		if (Boolean(title) === false) {
			return;
		}
		uni.showToast({
			title,
			duration,
			mask,
			icon
		});
	},

	//提示 请求中
	msgLoading: function (mask = true) {
		uni.showLoading({
			mask: mask
		});
	},

	//必填验证
	IsEmpty: function (str) {
		return (str == null || str.length < 1) ? false : true;
	},
};